package com.orcamento.dao;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.orcamento.entity.CreditCard;
import com.orcamento.entity.Invoice;
import com.orcamento.entity.enums.Month;

public class InvoiceDao extends AbstractDao<Invoice> {

	public InvoiceDao() {
		super(Invoice.class);
	}

	public Invoice save(Invoice invoice) {
		return super.saveEntity(invoice);
	}

	public Invoice findByIdAndAccount(Long id, Long accountId) {
		return super.findByIdAndAccount(id, accountId);
	}

	public Invoice findByMonthAndYearAndAccount(Month month, Integer year, CreditCard creditCard, Long accountId) {
		Criteria criteria = session().createCriteria( Invoice.class );
		criteria.createAlias("account", "account");
		
		criteria.add( Restrictions.eq("month", month.id) );
		criteria.add( Restrictions.eq("year", year) );
		criteria.add( Restrictions.eq("creditCard", creditCard) );
		criteria.add( Restrictions.eq("account.id", accountId) );
		
		return (Invoice) criteria.uniqueResult();
	}

}